package org.bpms.service.admin;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.bpms.model.admin.Faqs;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.plugin.redis.Redis;

/**
 * 问答模块操作接口
 * @author hupei
 *
 */
public class FaqService {
	
	/**
	 * 保存问答或者回复
	 * @param faq
	 */
	@Before(Tx.class)
	public void saveOrUpdate(Faqs faq){
		//清空缓存
		Set<String> keys = Redis.use().keys("*faqlist*");
		Iterator<String> it = keys.iterator();
		while (it.hasNext()) {
			String key = it.next(); 
			Redis.use().del(key);
		}	
		
		if (null != faq.getInt("id") && faq.getInt("id") != 0){
			faq.update();
			Redis.use().del("faq_"+faq.getInt("id"));
		}else{
			faq.save();
		}
		if (null != faq.getInt("pid")){//更新回复数量
			//清除回复缓存
			Set<String> ckeys = Redis.use().keys("*faqchild*");
			Iterator<String> cit = ckeys.iterator();
			while (cit.hasNext()) {
				String key = cit.next();  
				Redis.use().del(key);
			}				
			Db.update("update faqs set replys=replys+1 where id=?" , faq.getInt("pid"));
		}
		
	}
	
	/**
	 * 批量删除
	 * @param details
	 */
	@Before(Tx.class)
    public void remove(String details){
    	String [] detail = details.split(",");
    	for (String id:detail){
    		List<Faqs> lf = Faqs.me.find("select * from faqs where pid=?" , Integer.parseInt(id));
    		for (Faqs faq:lf){
    			Db.update("delete from faqcollection where faqid=?" , faq.getInt("id"));
    			Db.update("delete from faqpraise where faqid=?" , faq.getInt("id"));
    		}
    		Db.update("delete from faqs where pid=?" , Integer.parseInt(id));
    		Faqs.me.deleteById(Integer.parseInt(id));
    	}
    	//清空缓存
		Set<String> keys = Redis.use().keys("*faq*");
		Iterator<String> it = keys.iterator();
		while (it.hasNext()) {
			String key = it.next(); 
			Redis.use().del(key);
		}
    }
	
	/**
	 * 单个删除
	 * @param id
	 */
	@Before(Tx.class)
	public void del(int id){
		Record record = Db.findById("faqs", id);
		if (null == record.getInt("pid")){
			Db.update("delete from follow where faqid=?" , id);
			Db.update("delete from faqs where id=?" , id);
			List<Record> lr = Db.find("select * from faqs where pid=?" , id);
			for (Record r:lr){
				Db.update("delete from faqcollection where faqid=?" , r.getInt("id"));
				Db.update("delete from faqpraise where faqid=?" , r.getInt("id"));
				Db.update("delete from faqs where id=?" , r.getInt("id"));
			}
		}else{
			Db.update("update faqs set replys=replys-1 where id=?" , record.getInt("pid"));
			Db.update("delete from faqcollection where faqid=?" , id);
			Db.update("delete from faqpraise where faqid=?" , id);
			Db.update("delete from faqs where id=?" , id);
		}
    	//清空缓存
		Set<String> keys = Redis.use().keys("*faq*");
		Iterator<String> it = keys.iterator();
		while (it.hasNext()) {
			String key = it.next(); 
			Redis.use().del(key);
		}
	}
	
	/**
	 * 后台分页查询所有主题
	 * @param title
	 * @param stime
	 * @param etime
	 * @param pageNumber
	 * @param pageSize
	 * @param orderField
	 * @param orderDirection
	 * @param pagemap
	 */
	public void findAllTop(String title ,Integer typeid ,  String stime , String etime , Integer pageNumber,Integer pageSize,
	           String orderField,String orderDirection,Map<String,Object> pagemap){
		StringBuffer sql = new StringBuffer("from faqs a,member b where 1=1 and pid is null ");
		if (!stime.equals("")){
			sql.append(" and a.ctime >='"+stime+"'");
		}
		
		if (!etime.equals("")){
			sql.append(" and a.ctime <='"+etime+"'");
		}	
		if (!title.equals("")){
			sql.append(" and a.title like '%"+title+"%'");
		}
		if (typeid != 0){
			sql.append(" and a.typeid="+typeid);
		}
		sql.append(" and a.mid=b.id ");
		sql.append(" order by "+orderField+" "+orderDirection);	
		Page<Record> pf = null;
		if (Redis.use().get(typeid+title+stime+"faqlist"+etime+"_"+pageNumber+"_"+pageSize+"_"+orderField+"_"+orderDirection) != null){
			pf = Redis.use().get(typeid+title+stime+"faqlist"+etime+"_"+pageNumber+"_"+pageSize+"_"+orderField+"_"+orderDirection);
		}else{
			pf = Db.paginate(pageNumber, pageSize, "select a.*,b.nickname as nickname , b.headimgurl as img ", sql.toString());
			Redis.use().set(typeid+title+stime+"faqlist"+etime+"_"+pageNumber+"_"+pageSize+"_"+orderField+"_"+orderDirection, pf);
		}
		pagemap.put("pageSize", pf.getPageSize());//页面大小
		pagemap.put("totalRow", pf.getTotalRow());//总行数
		pagemap.put("pageCurrent", pf.getPageNumber());//当前页号
		pagemap.put("orderField", orderField);//排序列名
		pagemap.put("orderDirection", orderDirection);//排序方式
		pagemap.put("pageData", pf.getList());//页面数据
	}
	
	/**
	 * 后台分页查询会员收藏的主题
	 * @param title
	 * @param stime
	 * @param etime
	 * @param pageNumber
	 * @param pageSize
	 * @param orderField
	 * @param orderDirection
	 * @param pagemap
	 */
	public void findMyRAllTop(String mid , Integer pageNumber,Integer pageSize,
	           String orderField,String orderDirection,Map<String,Object> pagemap){
		StringBuffer sql = new StringBuffer("from faqs a,member b , faqcollection c where 1=1 and pid is null ");
		sql.append(" and a.mid=b.id ");
		sql.append(" and a.id=c.faqid and c.mid='"+mid+"'");
		sql.append(" order by "+orderField+" "+orderDirection);	
		Page<Record> pf = null;
		if (Redis.use().get("R_"+mid+"_"+"faqlist"+"_"+pageNumber+"_"+pageSize+"_"+orderField+"_"+orderDirection) != null){
			pf = Redis.use().get("R_"+mid+"_"+"faqlist"+"_"+pageNumber+"_"+pageSize+"_"+orderField+"_"+orderDirection);
		}else{
			pf = Db.paginate(pageNumber, pageSize, "select a.*,b.nickname as nickname , b.headimgurl as img ", sql.toString());
			Redis.use().set("R_"+mid+"_"+"faqlist"+"_"+pageNumber+"_"+pageSize+"_"+orderField+"_"+orderDirection, pf);
		}
		pagemap.put("pageSize", pf.getPageSize());//页面大小
		pagemap.put("totalRow", pf.getTotalRow());//总行数
		pagemap.put("pageCurrent", pf.getPageNumber());//当前页号
		pagemap.put("orderField", orderField);//排序列名
		pagemap.put("orderDirection", orderDirection);//排序方式
		pagemap.put("pageData", pf.getList());//页面数据
	}
	
	/**
	 * 后台分页查询会员的主题
	 * @param title
	 * @param stime
	 * @param etime
	 * @param pageNumber
	 * @param pageSize
	 * @param orderField
	 * @param orderDirection
	 * @param pagemap
	 */
	public void findMyAllTop(String mid , Integer pageNumber,Integer pageSize,
	           String orderField,String orderDirection,Map<String,Object> pagemap){
		StringBuffer sql = new StringBuffer("from faqs a,member b where 1=1 and pid is null ");
		sql.append(" and a.mid=b.id ");
		sql.append(" and a.mid='"+mid+"'");
		sql.append(" order by "+orderField+" "+orderDirection);	
		Page<Record> pf = null;
		if (Redis.use().get(mid+"faqlist"+"_"+pageNumber+"_"+pageSize+"_"+orderField+"_"+orderDirection) != null){
			pf = Redis.use().get(mid+"faqlist"+"_"+pageNumber+"_"+pageSize+"_"+orderField+"_"+orderDirection);
		}else{
			pf = Db.paginate(pageNumber, pageSize, "select a.*,b.nickname as nickname , b.headimgurl as img ", sql.toString());
			Redis.use().set(mid+"faqlist"+"_"+pageNumber+"_"+pageSize+"_"+orderField+"_"+orderDirection, pf);
		}
		pagemap.put("pageSize", pf.getPageSize());//页面大小
		pagemap.put("totalRow", pf.getTotalRow());//总行数
		pagemap.put("pageCurrent", pf.getPageNumber());//当前页号
		pagemap.put("orderField", orderField);//排序列名
		pagemap.put("orderDirection", orderDirection);//排序方式
		pagemap.put("pageData", pf.getList());//页面数据
	}
	
	/**
	 * 后台分页查询被会员回复的主题
	 * @param title
	 * @param stime
	 * @param etime
	 * @param pageNumber
	 * @param pageSize
	 * @param orderField
	 * @param orderDirection
	 * @param pagemap
	 */
	public void findMyReAllTop(String mid , Integer pageNumber,Integer pageSize,
	           String orderField,String orderDirection,Map<String,Object> pagemap){
		StringBuffer sql = new StringBuffer("from faqs a,member b where 1=1 and pid is null ");
		sql.append(" and a.mid=b.id ");
		sql.append(" and a.id in(select pid from faqs where pid is not null and mid='"+mid+"')");
		sql.append(" order by "+orderField+" "+orderDirection);	
		Page<Record> pf = null;
		if (Redis.use().get("Re_"+mid+"faqlist"+"_"+pageNumber+"_"+pageSize+"_"+orderField+"_"+orderDirection) != null){
			pf = Redis.use().get("Re_"+mid+"faqlist"+"_"+pageNumber+"_"+pageSize+"_"+orderField+"_"+orderDirection);
		}else{
			pf = Db.paginate(pageNumber, pageSize, "select a.*,b.nickname as nickname , b.headimgurl as img ", sql.toString());
			Redis.use().set("Re_"+mid+"faqlist"+"_"+pageNumber+"_"+pageSize+"_"+orderField+"_"+orderDirection, pf);
		}
		pagemap.put("pageSize", pf.getPageSize());//页面大小
		pagemap.put("totalRow", pf.getTotalRow());//总行数
		pagemap.put("pageCurrent", pf.getPageNumber());//当前页号
		pagemap.put("orderField", orderField);//排序列名
		pagemap.put("orderDirection", orderDirection);//排序方式
		pagemap.put("pageData", pf.getList());//页面数据
	}
	
	/**
	 * 得到主题的所有回复
	 * @param title
	 * @param stime
	 * @param etime
	 * @param pageNumber
	 * @param pageSize
	 * @param orderField
	 * @param orderDirection
	 * @param pagemap
	 */
	public void findAllChi(int id ,String mid , String type , Integer pageNumber,Integer pageSize,
	           String orderField,String orderDirection,Map<String,Object> pagemap){
		StringBuffer sql = new StringBuffer("from faqs a,member b where 1=1 ");
		sql.append(" and a.pid="+id);
		sql.append(" and a.mid=b.id ");
		if (type.equals("answer")){
			sql.append(" and a.mid='"+mid+"'");
		}else if(type.equals("collection")){
			sql.append(" and a.id in(select faqid from faqcollection where mid='"+mid+"')");
		}
		sql.append(" order by "+orderField+" "+orderDirection);	
		Page<Record> pf = null;
		if (Redis.use().get(type+"_"+mid+"_"+id+"_"+"faqchild"+"_"+pageNumber+"_"+pageSize+"_"+orderField+"_"+orderDirection) != null){
			pf = Redis.use().get(type+"_"+mid+"_"+id+"_"+"faqchild"+"_"+pageNumber+"_"+pageSize+"_"+orderField+"_"+orderDirection);
		}else{
			pf = Db.paginate(pageNumber, pageSize, "select a.*,b.nickname as nickname , b.headimgurl as img , b.autograph", sql.toString());
			Redis.use().set(type+"_"+mid+"_"+id+"_"+"faqchild"+"_"+pageNumber+"_"+pageSize+"_"+orderField+"_"+orderDirection, pf);
		}
		pagemap.put("pageSize", pf.getPageSize());//页面大小
		pagemap.put("totalRow", pf.getTotalRow());//总行数
		pagemap.put("pageCurrent", pf.getPageNumber());//当前页号
		pagemap.put("orderField", orderField);//排序列名
		pagemap.put("orderDirection", orderDirection);//排序方式
		pagemap.put("pageData", pf.getList());//页面数据
	}	
	
	/**
	 * 获取主题
	 * @return
	 */
	public Record findById(Integer id){
		Record faq = null;
		if (null != Redis.use().get("faq_"+id)){
			faq = Redis.use().get("faq_"+id);
		}else{
			faq = Db.findFirst("select a.*,b.nickname as nickname , b.headimgurl as img , b.autograph"
					+ " from faqs a,member b where a.mid=b.id and a.id=?" , id);
			if (null != faq){
				Redis.use().set("faq_"+id, faq);
			}
		}
		return faq;
	}
	
	
	/**
	 * 点赞回复
	 * @param id
	 * @param openid
	 * @return
	 */
	@Before(Tx.class)
	public int doPraise(int id , String openid){
		Record record = new Record();
		record.set("faqid", id).set("mid", openid);
		Db.save("faqpraise", record);
		Db.update("update faqs set praise=praise+1 where id=?" , id);
		Record record1 = Db.findById("faqs", id);
		Record record2 = Db.findById("faqs", record1.getInt("pid"));
		Db.update("update faqs set praise=praise+1 where id=?" , record2.getInt("id"));
    	//清空缓存
		Set<String> keys = Redis.use().keys("*faq*");
		Iterator<String> it = keys.iterator();
		while (it.hasNext()) {
			String key = it.next(); 
			Redis.use().del(key);
		}
		int sum = Db.findById("faqs", id).getInt("praise");
		return sum;
	}
	
	/**
	 * 取消点赞回复
	 * @param id
	 * @param openid
	 * @return
	 */
	@Before(Tx.class)
	public int doNoPraise(int id , String openid){
		Db.update("delete from faqpraise where mid=? and faqid=?" , openid , id);
		Db.update("update faqs set praise=praise-1 where id=?" , id);
		Record record1 = Db.findById("faqs", id);
		Record record2 = Db.findById("faqs", record1.getInt("pid"));
		Db.update("update faqs set praise=praise-1 where id=?" , record2.getInt("id"));
    	//清空缓存
		Set<String> keys = Redis.use().keys("*faq*");
		Iterator<String> it = keys.iterator();
		while (it.hasNext()) {
			String key = it.next(); 
			Redis.use().del(key);
		}
		int sum = Db.findById("faqs", id).getInt("praise");
		return sum;
	}
	
	/**
	 * 关注问题
	 * @param id
	 * @param openid
	 * @return
	 */
	@Before(Tx.class)
	public int doFollow(int id , String openid){
		Record record = new Record();
		record.set("faqid", id).set("mid", openid);
		Db.save("follow", record);
		Db.update("update faqs set follow=follow+1 where id=?" , id);
		
    	//清空缓存
		Set<String> keys = Redis.use().keys("*faq*");
		Iterator<String> it = keys.iterator();
		while (it.hasNext()) {
			String key = it.next(); 
			Redis.use().del(key);
		}
		int sum = Db.findById("faqs", id).getInt("follow");
		return sum;
	}
	
	/**
	 * 取消关注问题
	 * @param id
	 * @param openid
	 * @return
	 */
	@Before(Tx.class)
	public int doNotFollow(int id , String openid){
		Db.update("delete from follow where mid=? and faqid=?" , openid , id);
		Db.update("update faqs set follow=follow-1 where id=?" , id);
    	//清空缓存
		Set<String> keys = Redis.use().keys("*faq*");
		Iterator<String> it = keys.iterator();
		while (it.hasNext()) {
			String key = it.next(); 
			Redis.use().del(key);
		}
		int sum = Db.findById("faqs", id).getInt("follow");
		return sum;
	}
	
	/**
	 * 关注用户
	 * @param mid
	 * @param openid
	 * @return
	 */
	@Before(Tx.class)
	public int doFollowperson(String mid , String openid){
		Record record = new Record();
		record.set("bmid", mid).set("mid", openid);
		Db.save("followmember", record);
		String sql = "select count(*) from followmember where bmid=?";
		int sum = Db.queryNumber(sql , openid).intValue();
		return sum;
	}
	
	/**
	 * 取消关注用户
	 * @param id
	 * @param openid
	 * @return
	 */
	@Before(Tx.class)
	public int doNotFollowperson(String mid , String openid){
		Db.update("delete from followmember where bmid=? and mid=?" , mid , openid);
		String sql = "select count(*) from followmember where bmid=?";
		int sum = Db.queryNumber(sql , openid).intValue();
		return sum;
	}
	
	/**
	 * 收藏
	 * @param id
	 * @param openid
	 * @return
	 */
	@Before(Tx.class)
	public int doCollection(int id , String openid){
		Record record = new Record();
		record.set("faqid", id).set("mid", openid);
		Db.save("faqcollection", record);
		Db.update("update faqs set collections=collections+1 where id=?" , id);
		Record record1 = Db.findById("faqs", id);
		Record record2 = Db.findById("faqs", record1.getInt("pid"));
		Db.update("update faqs set collections=collections+1 where id=?" , record2.getInt("id"));
		
    	//清空缓存
		Set<String> keys = Redis.use().keys("*faq*");
		Iterator<String> it = keys.iterator();
		while (it.hasNext()) {
			String key = it.next(); 
			Redis.use().del(key);
		}
		int sum = Db.findById("faqs", id).getInt("collections");
		return sum;		
	}
	
	/**
	 * 取消收藏
	 * @param id
	 * @param openid
	 * @return
	 */
	@Before(Tx.class)
	public int doNoCollection(int id , String openid){
		Db.update("delete from faqcollection where mid=? and faqid=?" ,openid ,id);
		Db.update("update faqs set collections=collections-1 where id=?" , id);
		Record record1 = Db.findById("faqs", id);
		Record record2 = Db.findById("faqs", record1.getInt("pid"));
		Db.update("update faqs set collections=collections-1 where id=?" , record2.getInt("id"));
		
    	//清空缓存
		Set<String> keys = Redis.use().keys("*faq*");
		Iterator<String> it = keys.iterator();
		while (it.hasNext()) {
			String key = it.next(); 
			Redis.use().del(key);
		}
		int sum = Db.findById("faqs", id).getInt("collections");
		return sum;		
	}
	
	/**
	 * 查看是否是我的问题或者回复
	 * @param id
	 * @param openid
	 * @return
	 */
	public boolean ismy(int id , String openid){
		boolean ishave = false;
		List<Record> lr = Db.find("select * from faqs where id=? and mid=?" , id , openid);
		if (null != lr && lr.size() > 0){
			ishave = true;
		}
		return ishave;
	}
	
	/**
	 * 查询是否点赞回复
	 * @param id
	 * @param openid
	 * @return
	 */
	public boolean isPraise(int id , String openid){
		boolean praise = false;
		List<Record> lr = Db.find("select * from faqpraise where faqid=? and mid=?" , id , openid);
		if (null != lr && lr.size() > 0){
			praise = true;
		}
		return praise;
	}
	
	
	
	/**
	 * 查询是否收藏回复
	 * @param id
	 * @param openid
	 * @return
	 */
	public boolean isCollection(int id , String openid){
		boolean praise = false;
		List<Record> lr = Db.find("select * from faqcollection where faqid=? and mid=?" , id , openid);
		if (null != lr && lr.size() > 0){
			praise = true;
		}
		return praise;
	}	
	
	/**
	 * 查询是否关注问题
	 * @param id
	 * @param openid
	 * @return
	 */
	public boolean isFollow(int id , String openid){
		boolean praise = false;
		List<Record> lr = Db.find("select * from follow where faqid=? and mid=?" , id , openid);
		if (null != lr && lr.size() > 0){
			praise = true;
		}
		return praise;
	}
	
	/**
	 * 查询是否关注问题
	 * @param id
	 * @param openid
	 * @return
	 */
	public boolean isFollowMember(String mid , String bmid){
		boolean praise = false;
		List<Record> lr = Db.find("select * from followmember where mid=? and bmid=?" , mid , bmid);
		if (null != lr && lr.size() > 0){
			praise = true;
		}
		return praise;
	}
	
	/**
	 * 查询所有被关注的问题
	 * @param mid
	 * @param pageNumber
	 * @param pageSize
	 * @param orderField
	 * @param orderDirection
	 * @param pagemap
	 */
	public void findMyAllFollow(String mid , Integer pageNumber,Integer pageSize,
	           String orderField,String orderDirection,Map<String,Object> pagemap){
		StringBuffer sql = new StringBuffer("from faqs a,member b where a.pid is null and a.id in(select faqid from follow where mid='"+mid+"')");
		sql.append(" and a.mid=b.id ");
		sql.append(" order by "+orderField+" "+orderDirection);	
		Page<Record> pf = null;
		if (Redis.use().get("f_"+mid+"faqlist"+"_"+pageNumber+"_"+pageSize+"_"+orderField+"_"+orderDirection) != null){
			pf = Redis.use().get("f_"+mid+"faqlist"+"_"+pageNumber+"_"+pageSize+"_"+orderField+"_"+orderDirection);
		}else{
			pf = Db.paginate(pageNumber, pageSize, "select a.*,b.nickname as nickname , b.headimgurl as img ", sql.toString());
			Redis.use().set("f_"+mid+"faqlist"+"_"+pageNumber+"_"+pageSize+"_"+orderField+"_"+orderDirection, pf);
		}
		pagemap.put("pageSize", pf.getPageSize());//页面大小
		pagemap.put("totalRow", pf.getTotalRow());//总行数
		pagemap.put("pageCurrent", pf.getPageNumber());//当前页号
		pagemap.put("orderField", orderField);//排序列名
		pagemap.put("orderDirection", orderDirection);//排序方式
		pagemap.put("pageData", pf.getList());//页面数据
	}
	
	/**
	 * 查询所有回答过的的问题
	 * @param mid
	 * @param pageNumber
	 * @param pageSize
	 * @param orderField
	 * @param orderDirection
	 * @param pagemap
	 */
	public void findMyAllAnwser(String mid , Integer pageNumber,Integer pageSize,
	           String orderField,String orderDirection,Map<String,Object> pagemap){
		StringBuffer sql = new StringBuffer("from faqs a,member b where a.pid is null and a.id in(select pid from follow where pid is not null and mid='"+mid+"')");
		sql.append(" and a.mid=b.id ");
		sql.append(" order by "+orderField+" "+orderDirection);	
		Page<Record> pf = null;
		if (Redis.use().get("af_"+mid+"faqlist"+"_"+pageNumber+"_"+pageSize+"_"+orderField+"_"+orderDirection) != null){
			pf = Redis.use().get("af_"+mid+"faqlist"+"_"+pageNumber+"_"+pageSize+"_"+orderField+"_"+orderDirection);
		}else{
			pf = Db.paginate(pageNumber, pageSize, "select a.*,b.nickname as nickname , b.headimgurl as img ", sql.toString());
			Redis.use().set("af_"+mid+"faqlist"+"_"+pageNumber+"_"+pageSize+"_"+orderField+"_"+orderDirection, pf);
		}
		pagemap.put("pageSize", pf.getPageSize());//页面大小
		pagemap.put("totalRow", pf.getTotalRow());//总行数
		pagemap.put("pageCurrent", pf.getPageNumber());//当前页号
		pagemap.put("orderField", orderField);//排序列名
		pagemap.put("orderDirection", orderDirection);//排序方式
		pagemap.put("pageData", pf.getList());//页面数据
	}
	
	/**
	 * 查询所有被收藏的问题
	 * @param mid
	 * @param pageNumber
	 * @param pageSize
	 * @param orderField
	 * @param orderDirection
	 * @param pagemap
	 */
	public void findMyAllCollection(String mid , Integer pageNumber,Integer pageSize,
	           String orderField,String orderDirection,Map<String,Object> pagemap){
		StringBuffer sql = new StringBuffer("from faqs a,member b where a.pid is null and a.id in(select pid from faqs where id in(select faqid from faqcollection where mid='"+mid+"'))");
		sql.append(" and a.mid=b.id ");
		sql.append(" order by "+orderField+" "+orderDirection);	
		Page<Record> pf = null;
		if (Redis.use().get("cf_"+mid+"faqlist"+"_"+pageNumber+"_"+pageSize+"_"+orderField+"_"+orderDirection) != null){
			pf = Redis.use().get("cf_"+mid+"faqlist"+"_"+pageNumber+"_"+pageSize+"_"+orderField+"_"+orderDirection);
		}else{
			pf = Db.paginate(pageNumber, pageSize, "select a.*,b.nickname as nickname , b.headimgurl as img ", sql.toString());
			Redis.use().set("cf_"+mid+"faqlist"+"_"+pageNumber+"_"+pageSize+"_"+orderField+"_"+orderDirection, pf);
		}
		pagemap.put("pageSize", pf.getPageSize());//页面大小
		pagemap.put("totalRow", pf.getTotalRow());//总行数
		pagemap.put("pageCurrent", pf.getPageNumber());//当前页号
		pagemap.put("orderField", orderField);//排序列名
		pagemap.put("orderDirection", orderDirection);//排序方式
		pagemap.put("pageData", pf.getList());//页面数据
	}
}
